/*     */ package system.staros.sblock1;
/*     */ 
/*     */ import java.util.LinkedList;
/*     */ import java.util.List;
/*     */ 
/*     */ public class Parser
/*     */ {
/*     */   TokenBuffer lookAheadBuffer;
/*     */ 
/*     */   public Parser(Lexer paramLexer)
/*     */   {
/*  39 */     this.lookAheadBuffer = new TokenBuffer(paramLexer, 2);
/*     */   }
/*     */ 
/*     */   private TokenType lookAhead(int paramInt) {
/*  43 */     if ((this.lookAheadBuffer.isEmpty()) || (paramInt > this.lookAheadBuffer.size())) {
/*  44 */       return null;
/*     */     }
/*  46 */     Token localToken = this.lookAheadBuffer.getToken(paramInt - 1);
/*  47 */     return localToken.getType();
/*     */   }
/*     */ 
/*     */   private Token match(TokenType paramTokenType) {
/*  51 */     Token localToken = this.lookAheadBuffer.readToken();
/*  52 */     if (localToken == null) {
/*  53 */       throw new ParserException("Expecting type " + paramTokenType + " but didn't get a token");
/*     */     }
/*  55 */     if (localToken.getType() != paramTokenType) {
/*  56 */       throw new ParserException("Expecting type " + paramTokenType + " but got " + localToken.getType(), localToken.getPosition());
/*     */     }
/*  58 */     return localToken;
/*     */   }
/*     */ 
/*     */   public RootNode program() {
/*  62 */     LinkedList localLinkedList = new LinkedList();
/*  63 */     while (lookAhead(1) != null) {
/*  64 */       localLinkedList.add(statement());
/*     */     }
/*  66 */     return new RootNode(new SourcePosition(1, 1), localLinkedList);
/*     */   }
/*     */ 
/*     */   private BlockNode block()
/*     */   {
/*  71 */     Token localToken = match(TokenType.LBRACE);
/*  72 */     LinkedList localLinkedList = new LinkedList();
/*  73 */     while (lookAhead(1) != TokenType.RBRACE) {
/*  74 */       localLinkedList.add(statement());
/*     */     }
/*  76 */     match(TokenType.RBRACE);
/*  77 */     return new BlockNode(localToken.getPosition(), localLinkedList);
/*     */   }
/*     */ 
/*     */   private Node statement()
/*     */   {
/*  85 */     TokenType localTokenType = lookAhead(1);
/*     */     Object localObject1;
/*  86 */     if ((localTokenType == TokenType.VARIABLE) && (lookAhead(2) == TokenType.LPAREN)) {
/*  87 */       localObject1 = functionCall();
/*  88 */       match(TokenType.END_STATEMENT);
/*  89 */       return localObject1;
/*     */     }
/*     */     Object localObject2;
/*  90 */     if (localTokenType == TokenType.VARIABLE) {
/*  91 */       localObject1 = variable();
/*  92 */       localObject2 = match(TokenType.ASSIGN).getPosition();
/*  93 */       Node localNode = expression();
/*  94 */       match(TokenType.END_STATEMENT);
/*  95 */       return new AssignNode((SourcePosition)localObject2, (Node)localObject1, localNode);
/*  96 */     }if (localTokenType == TokenType.RETURN) {
/*  97 */       localObject1 = match(TokenType.RETURN).getPosition();
/*  98 */       localObject2 = expression();
/*  99 */       match(TokenType.END_STATEMENT);
/* 100 */       return new ReturnNode((SourcePosition)localObject1, (Node)localObject2);
/* 101 */     }if (localTokenType == TokenType.IF)
/* 102 */       return _if();
/* 103 */     if (localTokenType == TokenType.WHILE)
/* 104 */       return _while();
/* 105 */     if (localTokenType == TokenType.FOR_EACH) {
/* 106 */       return foreach();
/*     */     }
/*     */ 
/* 110 */     throw new ParserException("Unknown token type " + localTokenType);
/*     */   }
/*     */ 
/*     */   private Node condition()
/*     */   {
/* 115 */     match(TokenType.LPAREN);
/* 116 */     Node localNode = booleanExpression();
/* 117 */     match(TokenType.RPAREN);
/* 118 */     return localNode;
/*     */   }
/*     */ 
/*     */   private Node _if()
/*     */   {
/* 123 */     SourcePosition localSourcePosition = match(TokenType.IF).getPosition();
/* 124 */     Node localNode1 = condition();
/* 125 */     BlockNode localBlockNode = block();
/* 126 */     Node localNode2 = null;
/* 127 */     if (lookAhead(1) == TokenType.ELSE) {
/* 128 */       localNode2 = _else();
/*     */     }
/* 130 */     return new IfNode(localSourcePosition, localNode1, localBlockNode, localNode2);
/*     */   }
/*     */ 
/*     */   private Node _else()
/*     */   {
/* 135 */     match(TokenType.ELSE);
/* 136 */     if (lookAhead(1) == TokenType.IF) {
/* 137 */       return _if();
/*     */     }
/* 139 */     return block();
/*     */   }
/*     */ 
/*     */   private Node _while()
/*     */   {
/* 145 */     SourcePosition localSourcePosition = match(TokenType.WHILE).getPosition();
/* 146 */     Node localNode = condition();
/* 147 */     BlockNode localBlockNode = block();
/* 148 */     return new WhileNode(localSourcePosition, localNode, localBlockNode);
/*     */   }
/*     */ 
/*     */   private Node foreach()
/*     */   {
/* 154 */     SourcePosition localSourcePosition = match(TokenType.FOR_EACH).getPosition();
/* 155 */     match(TokenType.LPAREN);
/* 156 */     Token localToken = match(TokenType.VARIABLE);
/* 157 */     VariableNode localVariableNode1 = new VariableNode(localToken.getPosition(), localToken.getText());
/* 158 */     match(TokenType.AS);
/* 159 */     localToken = match(TokenType.VARIABLE);
/* 160 */     VariableNode localVariableNode2 = new VariableNode(localToken.getPosition(), localToken.getText());
/* 161 */     Object localObject1 = localVariableNode2;
/* 162 */     if (lookAhead(1) == TokenType.COLON) {
/* 163 */       localObject2 = match(TokenType.COLON).getPosition();
/* 164 */       VariableNode localVariableNode3 = localVariableNode2;
/* 165 */       localToken = match(TokenType.VARIABLE);
/* 166 */       localVariableNode2 = new VariableNode(localToken.getPosition(), localToken.getText());
/* 167 */       localObject1 = new DictionaryEntryNode((SourcePosition)localObject2, localVariableNode3, localVariableNode2);
/*     */     }
/* 169 */     match(TokenType.RPAREN);
/* 170 */     Object localObject2 = block();
/* 171 */     return new ForeachNode(localSourcePosition, localVariableNode1, (Node)localObject1, (Node)localObject2);
/*     */   }
/*     */ 
/*     */   private Node array()
/*     */   {
/* 176 */     SourcePosition localSourcePosition = match(TokenType.LBRACKET).getPosition();
/* 177 */     LinkedList localLinkedList = new LinkedList();
/* 178 */     if (lookAhead(1) != TokenType.RBRACKET) {
/* 179 */       localLinkedList.add(expression());
/* 180 */       while (lookAhead(1) == TokenType.COMMA) {
/* 181 */         match(TokenType.COMMA);
/* 182 */         localLinkedList.add(expression());
/*     */       }
/*     */     }
/* 185 */     match(TokenType.RBRACKET);
/* 186 */     return new ArrayNode(localSourcePosition, localLinkedList);
/*     */   }
/*     */ 
/*     */   private DictionaryNode dictionary()
/*     */   {
/* 191 */     SourcePosition localSourcePosition = match(TokenType.LBRACE).getPosition();
/* 192 */     LinkedList localLinkedList = new LinkedList();
/* 193 */     if (lookAhead(1) != TokenType.RBRACE) {
/* 194 */       localLinkedList.add(keyValue());
/* 195 */       while (lookAhead(1) == TokenType.COMMA) {
/* 196 */         match(TokenType.COMMA);
/* 197 */         localLinkedList.add(keyValue());
/*     */       }
/*     */     }
/* 200 */     match(TokenType.RBRACE);
/* 201 */     return new DictionaryNode(localSourcePosition, localLinkedList);
/*     */   }
/*     */ 
/*     */   private DictionaryEntryNode keyValue()
/*     */   {
/* 206 */     Node localNode1 = key();
/* 207 */     SourcePosition localSourcePosition = match(TokenType.COLON).getPosition();
/* 208 */     Node localNode2 = expression();
/* 209 */     return new DictionaryEntryNode(localSourcePosition, localNode1, localNode2);
/*     */   }
/*     */ 
/*     */   private Node key()
/*     */   {
/* 214 */     if (lookAhead(1) == TokenType.STRING_LITERAL) {
/* 215 */       localToken = match(TokenType.STRING_LITERAL);
/* 216 */       return new StringNode(localToken.getPosition(), localToken.getText());
/*     */     }
/* 218 */     Token localToken = match(TokenType.NUMBER);
/* 219 */     return new NumberNode(localToken.getPosition(), localToken.getText());
/*     */   }
/*     */ 
/*     */   private FunctionNode function()
/*     */   {
/* 226 */     SourcePosition localSourcePosition = match(TokenType.FUNCTION).getPosition();
/* 227 */     match(TokenType.LPAREN);
/* 228 */     List localList = FunctionNode.NO_PARAMETERS;
/* 229 */     if (lookAhead(1) != TokenType.RPAREN) {
/* 230 */       localList = parameterList();
/*     */     }
/* 232 */     match(TokenType.RPAREN);
/* 233 */     BlockNode localBlockNode = block();
/* 234 */     return new FunctionNode(localSourcePosition, localList, localBlockNode);
/*     */   }
/*     */ 
/*     */   private List<Node> parameterList()
/*     */   {
/* 239 */     LinkedList localLinkedList = new LinkedList();
/* 240 */     localLinkedList.add(parameter());
/* 241 */     while (lookAhead(1) == TokenType.COMMA) {
/* 242 */       match(TokenType.COMMA);
/* 243 */       localLinkedList.add(parameter());
/*     */     }
/* 245 */     return localLinkedList;
/*     */   }
/*     */ 
/*     */   private Node parameter()
/*     */   {
/* 250 */     Token localToken = match(TokenType.VARIABLE);
/* 251 */     VariableNode localVariableNode = new VariableNode(localToken.getPosition(), localToken.getText());
/* 252 */     if (lookAhead(1) == TokenType.ASSIGN) {
/* 253 */       SourcePosition localSourcePosition = match(TokenType.ASSIGN).getPosition();
/* 254 */       Node localNode = expression();
/* 255 */       return new AssignNode(localSourcePosition, localVariableNode, localNode);
/*     */     }
/* 257 */     return localVariableNode;
/*     */   }
/*     */ 
/*     */   private Node expression() {
/* 261 */     TokenType localTokenType = lookAhead(1);
/* 262 */     if (localTokenType == TokenType.FUNCTION)
/* 263 */       return function();
/* 264 */     if (localTokenType == TokenType.LBRACKET)
/* 265 */       return array();
/* 266 */     if (localTokenType == TokenType.LBRACE) {
/* 267 */       return dictionary();
/*     */     }
/*     */ 
/* 270 */     return stringExpression();
/*     */   }
/*     */ 
/*     */   private Node sumExpression()
/*     */   {
/* 276 */     Object localObject = term();
/* 277 */     while ((lookAhead(1) == TokenType.PLUS) || (lookAhead(1) == TokenType.MINUS))
/*     */     {
/* 279 */       if (lookAhead(1) == TokenType.PLUS) {
/* 280 */         localObject = new AddOpNode(match(TokenType.PLUS).getPosition(), (Node)localObject, term());
/*     */       }
/* 282 */       else if (lookAhead(1) == TokenType.MINUS) {
/* 283 */         localObject = new SubtractOpNode(match(TokenType.MINUS).getPosition(), (Node)localObject, term());
/*     */       }
/*     */     }
/*     */ 
/* 287 */     return localObject;
/*     */   }
/*     */ 
/*     */   private Node term()
/*     */   {
/* 292 */     Object localObject = factor();
/*     */ 
/* 294 */     while ((lookAhead(1) == TokenType.MULTIPLY) || (lookAhead(1) == TokenType.DIVIDE) || (lookAhead(1) == TokenType.MOD))
/*     */     {
/* 296 */       if (lookAhead(1) == TokenType.MULTIPLY) {
/* 297 */         localObject = new MultiplyOpNode(match(TokenType.MULTIPLY).getPosition(), (Node)localObject, factor());
/*     */       }
/* 300 */       else if (lookAhead(1) == TokenType.DIVIDE) {
/* 301 */         localObject = new DivideOpNode(match(TokenType.DIVIDE).getPosition(), (Node)localObject, factor());
/*     */       }
/* 304 */       else if (lookAhead(1) == TokenType.MOD) {
/* 305 */         localObject = new ModOpNode(match(TokenType.MOD).getPosition(), (Node)localObject, factor());
/*     */       }
/*     */ 
/*     */     }
/*     */ 
/* 310 */     return localObject;
/*     */   }
/*     */ 
/*     */   private Node factor()
/*     */   {
/* 315 */     Object localObject = signExpression();
/* 316 */     while (lookAhead(1) == TokenType.POWER) {
/* 317 */       localObject = new PowerOpNode(match(TokenType.POWER).getPosition(), (Node)localObject, signExpression());
/*     */     }
/*     */ 
/* 320 */     return localObject;
/*     */   }
/*     */ 
/*     */   private Node signExpression()
/*     */   {
/* 325 */     Token localToken = null;
/* 326 */     if (lookAhead(1) == TokenType.MINUS)
/* 327 */       localToken = match(TokenType.MINUS);
/* 328 */     else if (lookAhead(1) == TokenType.PLUS) {
/* 329 */       match(TokenType.PLUS);
/*     */     }
/* 331 */     Node localNode = value();
/* 332 */     if (localToken != null) {
/* 333 */       return new NegateOpNode(localToken.getPosition(), localNode);
/*     */     }
/* 335 */     return localNode;
/*     */   }
/*     */ 
/*     */   private Node value()
/*     */   {
/* 341 */     if ((lookAhead(1) == TokenType.VARIABLE) && (lookAhead(2) == TokenType.LPAREN)) {
/* 342 */       return functionCall();
/*     */     }
/* 344 */     return atom();
/*     */   }
/*     */ 
/*     */   private Node functionCall()
/*     */   {
/* 350 */     Token localToken = match(TokenType.VARIABLE);
/* 351 */     String str = localToken.getText();
/* 352 */     match(TokenType.LPAREN);
/* 353 */     List localList = FunctionCallNode.NO_ARGUMENTS;
/* 354 */     if (lookAhead(1) != TokenType.RPAREN) {
/* 355 */       localList = argumentList();
/*     */     }
/* 357 */     match(TokenType.RPAREN);
/* 358 */     return new FunctionCallNode(localToken.getPosition(), str, localList);
/*     */   }
/*     */ 
/*     */   private List<Node> argumentList()
/*     */   {
/* 363 */     LinkedList localLinkedList = new LinkedList();
/* 364 */     localLinkedList.add(expression());
/* 365 */     while (lookAhead(1) == TokenType.COMMA) {
/* 366 */       match(TokenType.COMMA);
/* 367 */       localLinkedList.add(expression());
/*     */     }
/* 369 */     return localLinkedList;
/*     */   }
/*     */ 
/*     */   private Node atom()
/*     */   {
/* 377 */     TokenType localTokenType = lookAhead(1);
/*     */     Object localObject;
/* 378 */     if (localTokenType == TokenType.NUMBER) {
/* 379 */       localObject = match(TokenType.NUMBER);
/* 380 */       return new NumberNode(((Token)localObject).getPosition(), ((Token)localObject).getText());
/* 381 */     }if (localTokenType == TokenType.TRUE)
/* 382 */       return new TrueNode(match(TokenType.TRUE).getPosition());
/* 383 */     if (localTokenType == TokenType.FALSE)
/* 384 */       return new FalseNode(match(TokenType.FALSE).getPosition());
/* 385 */     if (localTokenType == TokenType.LPAREN) {
/* 386 */       match(TokenType.LPAREN);
/* 387 */       localObject = expression();
/* 388 */       match(TokenType.RPAREN);
/* 389 */       return localObject;
/*     */     }
/* 391 */     return variable();
/*     */   }
/*     */ 
/*     */   private Node variable()
/*     */   {
/* 396 */     Token localToken = match(TokenType.VARIABLE);
/* 397 */     VariableNode localVariableNode = new VariableNode(localToken.getPosition(), localToken.getText());
/* 398 */     if (lookAhead(1) == TokenType.LBRACKET) {
/* 399 */       SourcePosition localSourcePosition = match(TokenType.LBRACKET).getPosition();
/* 400 */       Node localNode = expression();
/* 401 */       match(TokenType.RBRACKET);
/* 402 */       return new LookupNode(localSourcePosition, (VariableNode)localVariableNode, localNode);
/*     */     }
/* 404 */     return localVariableNode;
/*     */   }
/*     */ 
/*     */   private Node booleanExpression()
/*     */   {
/* 410 */     Node localNode = booleanTerm();
/* 411 */     if (lookAhead(1) == TokenType.OR) {
/* 412 */       return new OrOpNode(match(TokenType.OR).getPosition(), localNode, booleanExpression());
/*     */     }
/* 414 */     return localNode;
/*     */   }
/*     */ 
/*     */   private Node booleanTerm()
/*     */   {
/* 419 */     Node localNode = booleanFactor();
/* 420 */     if (lookAhead(1) == TokenType.AND) {
/* 421 */       return new AndOpNode(match(TokenType.AND).getPosition(), localNode, booleanTerm());
/*     */     }
/* 423 */     return localNode;
/*     */   }
/*     */ 
/*     */   private Node booleanFactor()
/*     */   {
/* 428 */     if (lookAhead(1) == TokenType.NOT) {
/* 429 */       return new NotOpNode(match(TokenType.NOT).getPosition(), booleanRelation());
/*     */     }
/* 431 */     return booleanRelation();
/*     */   }
/*     */ 
/*     */   private Node booleanRelation()
/*     */   {
/* 436 */     Node localNode = sumExpression();
/* 437 */     TokenType localTokenType = lookAhead(1);
/* 438 */     if (localTokenType == TokenType.LESS_EQUAL) {
/* 439 */       return new LessEqualOpNode(match(TokenType.LESS_EQUAL).getPosition(), localNode, sumExpression());
/*     */     }
/* 441 */     if (localTokenType == TokenType.LESS_THEN) {
/* 442 */       return new LessThenOpNode(match(TokenType.LESS_THEN).getPosition(), localNode, sumExpression());
/*     */     }
/* 444 */     if (localTokenType == TokenType.GREATER_EQUAL) {
/* 445 */       return new GreaterEqualOpNode(match(TokenType.GREATER_EQUAL).getPosition(), localNode, sumExpression());
/*     */     }
/* 447 */     if (localTokenType == TokenType.GREATER_THEN) {
/* 448 */       return new GreaterThenOpNode(match(TokenType.GREATER_THEN).getPosition(), localNode, sumExpression());
/*     */     }
/* 450 */     if (localTokenType == TokenType.EQUAL) {
/* 451 */       return new EqualsOpNode(match(TokenType.EQUAL).getPosition(), localNode, sumExpression());
/*     */     }
/* 453 */     if (localTokenType == TokenType.NOT_EQUAL) {
/* 454 */       return new NotEqualsOpNode(match(TokenType.NOT_EQUAL).getPosition(), localNode, sumExpression());
/*     */     }
/*     */ 
/* 457 */     return localNode;
/*     */   }
/*     */ 
/*     */   private Node stringExpression()
/*     */   {
/* 462 */     Node localNode = string();
/* 463 */     if (lookAhead(1) == TokenType.CONCAT) {
/* 464 */       return new ConcatOpNode(match(TokenType.CONCAT).getPosition(), localNode, stringExpression());
/*     */     }
/*     */ 
/* 467 */     return localNode;
/*     */   }
/*     */ 
/*     */   private Node string()
/*     */   {
/* 472 */     if (lookAhead(1) == TokenType.STRING_LITERAL) {
/* 473 */       Token localToken = match(TokenType.STRING_LITERAL);
/* 474 */       return new StringNode(localToken.getPosition(), localToken.getText());
/*     */     }
/* 476 */     return booleanExpression();
/*     */   }
/*     */ }

/* Location:           D:\Марк\!IMPORTANT\staros\archive\version-2.260.zip
 * Qualified Name:     system.staros.sblock1.Parser
 * JD-Core Version:    0.6.1
 */